package com.fay.designPattern.oop.virtualWallet.rich;

import com.fay.designPattern.oop.virtualWallet.anaemic.InvalidAmountException;
import com.fay.designPattern.oop.virtualWallet.anaemic.NoSufficientBalanceException;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * @Title:
 * @Description: Domain领域模型 充血模型
 * @Author Fay Wang
 * @Date 2022/2/24 14:05
 * @Version 1.0
 */
@NoArgsConstructor
public class VirtualWallet {
    private Long id;
    private Long createTime = System.currentTimeMillis();
    private BigDecimal balance = BigDecimal.ZERO;
    private boolean isAllowedOverdraft = true;
    private BigDecimal overdraftAmount = BigDecimal.ZERO;
    private BigDecimal frozenAmount = BigDecimal.ZERO;

    public VirtualWallet(Long preAllocatedId) {
        this.id = preAllocatedId;
    }

    public void freeze(BigDecimal amount) {}
    public void unfreeze(BigDecimal amount){}
    public void increaseOverdraftAmount(BigDecimal amount){}
    public void decreaseOverdraftAmount(BigDecimal amount){}
    public void closeOverdraft(){}
    public void openOverdraft(){}



    public BigDecimal getBalance() {
        return this.balance;
    }

    public BigDecimal getAvailableBalance() {
        BigDecimal totalAvailableBalance = this.balance.subtract(this.frozenAmount);
        if (isAllowedOverdraft) {
            totalAvailableBalance = this.overdraftAmount.add(totalAvailableBalance);
        }
        return totalAvailableBalance;
    }

    public void debit(BigDecimal amount) throws NoSufficientBalanceException {
        if (this.balance.compareTo(amount) < 0) {
            throw new NoSufficientBalanceException();
        }
        this.balance.subtract(amount);
    }

    public void credit(BigDecimal amount) throws InvalidAmountException {
        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            throw new InvalidAmountException();
        }
        this.balance.add(amount);
    }

}
